package com.lzh.shijicloudstack.controller;

import com.lzh.shijicloudstack.annotation.LogAnnotation;
import com.lzh.shijicloudstack.common.LogType;
import com.lzh.shijicloudstack.common.R;
import com.lzh.shijicloudstack.common.ResultUtil;
import com.lzh.shijicloudstack.model.dto.user.*;
import com.lzh.shijicloudstack.model.entity.User;
import com.lzh.shijicloudstack.model.vo.user.LoginUserVo;
import com.lzh.shijicloudstack.service.UserService;
import com.lzh.shijicloudstack.utils.BeanCopyUtils;
import com.lzh.shijicloudstack.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 测试账号：
 * luzhiheng / $2a$10$ASwhRD2rRgDOJJPXELjNm.ZqI4YLRAa9JgzJKfvcT10C4tEBzOUXy
 *
 * @author luzhiheng
 * @description 用户控制器
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody LoginUserDto dto) {
        return userService.login(dto);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R regist(@RequestBody UserRegisterDto dto) {
        return userService.regist(dto);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public R logout() {
        return userService.logout();
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return userService.delete(id);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/get/login")
    public R<LoginUserVo> getLoginUser(HttpServletRequest request) {
        LoginUserVo user = userService.getLoginUser(request);
        return ResultUtil.success(user);
    }

    @LogAnnotation(message = "更新用户头像", operation = LogType.UPDATE)
    @PostMapping("/avatar")
    public R updateUserAvatar(Long userId, String avatar) {
        return userService.updateUserAvatar(userId, avatar);
    }

    @LogAnnotation(message = "更新用户信息", operation = LogType.UPDATE)
    @PutMapping("/updateUserInfo")
    public R updateUserInfo(@RequestBody UpdateUserDto dto) {
        User user = BeanCopyUtils.copyBean(dto, User.class);
        user.setId(SecurityUtils.getUserId());
        userService.updateById(user);
        return ResultUtil.success(true);
    }

    @LogAnnotation(message = "更新密码", operation = LogType.UPDATE)
    @PutMapping("/updatePassword")
    public R<?> updatePassword(@RequestBody UpdatePwdDto dto) {
        return userService.updatePasswordByUserId(dto);
    }

//    @LogAnnotation(message = "重置密码", operation = LogType.UPDATE)
//    @PutMapping("/rePassword")
//    public ResponseResult rePassword(@RequestBody UpdatePwdDTO user) {
//        return userService.rePassword(user.getEmail(), user.getNewPassword(), user.getConPassword());
//    }

    @LogAnnotation(message = "注销账号", operation = LogType.DELETE)
    @DeleteMapping("/cancel/{userId}")
    public R<Object> cancelAccount(@PathVariable Long userId) {
        return userService.cancelAccount(userId);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/pageList")
    public R<Map<String, Object>> pageList(@RequestBody UserPageDto dto) {
        return ResultUtil.success(userService.pageList(dto));
    }
}
